package com.yyk.yiliao.ui.trade.minepwd;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.ui.trade.widget.PasswordInputView;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankTestpwdInfo;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 设置 找回密码  已知道 交易密码的情况下
 * Android进入页面开始就自动弹出软键盘 - 杨成勇 - CSDN博客  https://blog.csdn.net/y_chengyong/article/details/42582583
 */
public class SetInvestPasswordActivity extends BaseActivity {

    public static SetInvestPasswordActivity intance;

    @BindView(R.id.pwd_inputview)
    PasswordInputView pwdInputview;
    @BindView(R.id.tv_forgetpwd)
    TextView tvForgetpwd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_invest_password;
    }

    @Override
    public void initView() {
        initToolbar();
        initTextListenter();
    }

    /**
     * 自动弹出键盘输入框
     */
    private void initRuanjianpan() {
        pwdInputview.setFocusable(true);
        pwdInputview.setFocusableInTouchMode(true);
        pwdInputview.requestFocus();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
                           public void run() {
                               InputMethodManager inputManager =
                                       (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                               inputManager.showSoftInput(pwdInputview, 0);
                           }
                       },
                200);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initRuanjianpan();
    }

    private void initTextListenter() {
        pwdInputview.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 5) {
                    String string = pwdInputview.getText().toString();
//                    Logger.e("string长度" + string.length());
                    if (string.length() == 6) {

                        Map<String, String> map = new TreeMap<>();

                        map.put("id", Hawk.get("uid") + "");
                        map.put("pwd", string + "");
                        map.put("sign", Md5Util2.createSign(map));
                        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankTestpwd(map)
                                .subscribeOn(Schedulers.io())     //调度线程
                                .observeOn(AndroidSchedulers.mainThread())
                                .unsubscribeOn(Schedulers.io())   //取消订阅
                                .subscribe(new Subscriber<UserbankTestpwdInfo>() {
                                    @Override
                                    public void onNext(UserbankTestpwdInfo info) {
                                        Logger.e("检验交易密码" + info.toString());
                                        if (info.getCode() == 1) {
                                            startAct(SetInvestPasswordUpadteActivity.class);
                                        } else if (info.getCode() == 2) {
                                            ToastUtil.showShort(mActivity, "密码错误");
                                        } else {
                                            ToastUtil.showShort(mActivity, info.getMsg());
                                        }
                                    }

                                    @Override
                                    public void onCompleted() {
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }
                                });
                    }

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("修改交易密码");
    }

    @Override
    public void initData() {
        intance = this;
    }


    @OnClick(R.id.tv_forgetpwd)
    public void onViewClicked() {
        startAct(FindInvestPasswordActivity.class);
    }
}
